package com.tzstcl.sys.user.service.impl;

import com.tzstcl.base.service.impl.BaseServiceImpl;
import com.tzstcl.sys.user.mapper.SysMenuMapper;
import com.tzstcl.sys.user.model.SysMenu;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author haocj
 * 用户service
 */
@Service
public class SysMenuService extends BaseServiceImpl<SysMenuMapper, SysMenu> {

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public SysMenu get(SysMenu sysMenu) {

        SysMenu sys = super.get(sysMenu);
        SysMenu menu = null;
        if (sys!=null&&sys.getParentId() != null&&sys.getParentId() !=0) {

            menu = super.getOne(sys.getParentId());
        }
        if (menu != null) {
            sys.setParentName(menu.getName());
        }
        return sys;
    }

    public List<SysMenu> findUserMenu(Long uid) {
        return sysMenuMapper.findUserMenu(uid);
    }

    public List<SysMenu> findListAllMenu() {
        return sysMenuMapper.findListAllMenu();
    }

    public List<SysMenu> findList(SysMenu sysMenu, Integer pageNum, Integer pageSize) {
        return sysMenuMapper.selectByPageNumSize(sysMenu, pageNum, pageSize);
    }

    public List<SysMenu> findListAll(SysMenu sysMenu) {
        List<SysMenu> list = sysMenuMapper.findListAll(sysMenu);
        if (list.size() > 0) {
            return list;
        }
        return new ArrayList<SysMenu>();
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void save(SysMenu sysMenu) {
        if (null==sysMenu.getId()) {
            sysMenu.preInit();
            sysMenuMapper.save(sysMenu);
        } else {
            sysMenu.preInit();
            super.update(sysMenu);
        }
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void delete(String[] ids) {
        sysMenuMapper.delete(ids);
    }
}
